<template>
  <div class="col-md-8">
    <h3 class="reply">评论回复：</h3>
    <h2  v-if="!commentData.length">暂无评论，点击左侧添加评论！！！</h2>
    <ul class="list-group">
      <CommentItem v-for="comment in commentData" :key="comment.id" :comment="comment" :delComments="delComments"/>
    </ul>
  </div>

</template>

<script>
  import CommentItem from "./CommentItem";
  export default {
    name: "ShowComment",
    components:{
      CommentItem
    },
    data() {
      return {
        commentData: [
          {id: 1, username: 'xiaoyu', content: 'react easy'},
          {id: 2, username: 'yutian', content: ' good'},
          {id: 3, username: 'qingtian', content: 'say hello'},
        ]
      }
    },
    mounted() {
      this.$bus.$on('addComments',this.addComments)
    },
    methods:{
      addComments(username, content) {
        this.commentData.unshift({
          id: Date.now(),
          username,
          content
        })
      },
      delComments(id) {
        const newCommentData = this.commentData.filter(item =>
          item.id !== id
        )
        this.commentData = newCommentData
        // console.log(this.commentData)
      }
    }
  }
</script>

<style scoped>

</style>